package com.walt.zhong.sample.views

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.Scroller

class CustomView @JvmOverloads
    constructor(
        context: Context,
        attrs:AttributeSet?=null,
        defStyleAttr:Int = 0
    ) : View(context,attrs,defStyleAttr){

    val mScroller = Scroller(context)
    var lastX = 0;
    var lastY = 0;

    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
        Log.d("zhongxj","dispatchTouchEvent======")
        return super.dispatchTouchEvent(event)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        Log.d("zhongxj","onTouchEvent======")
        val x = event.x.toInt()
        val y = event.y.toInt()

        when(event.action){
            MotionEvent.ACTION_DOWN ->{
                lastX = x
                lastY = y
            }

            MotionEvent.ACTION_MOVE->{
                val offSetX = x - lastX
                val offSetY = y - lastY

                layout(left + offSetX,top + offSetY,right + offSetX,bottom+offSetY)
            }

        }

        return true
    }
}